#!/bin/bash

json_data='[
    {
        "name": "云海",
        "command_gitee": "git clone https://gitee.com/wind-trace-typ/wind-plugin.git./plugins/wind-plugin",
        "command_github": "git clone https://github.com/example/wind-plugin.git./plugins/wind-plugin",
        "dep": "cd plugins && pnpm i && cd../"
    },
    {
        "name": "蓝天",
        "command_gitee": "git clone https://gitee.com/another/project.git./projects/another",
        "command_github": "git clone https://github.com/another/project.git./projects/another",
        "dep": "0"
    }
]'

# 解析 JSON 数据
IFS=$'\n'
json_array=($(echo "$json_data" | jq -c '.[]'))

# 创建选择仓库类型的菜单
repo_type=$(whiptail --title "选择仓库类型" --radiolist "请选择仓库类型" 15 60 2 "gitee" "Gitee" ON "github" "GitHub" OFF 3>&1 1>&2 2>&3)

# 创建项目选择菜单
options=()
for item in "${json_array[@]}"
do
    name=$(echo "$item" | jq -r '.name')
    options+=("$name")
done

# 使用 whiptail 创建菜单
selected_items=$(whiptail --title "选择安装项目" --checklist "请选择要安装的项目" 15 60 5 "${options[@]}" 3>&1 1>&2 2>&3)

# 处理选择的项目
for selected_item in $selected_items
do
    for item in "${json_array[@]}"
    do
        name=$(echo "$item" | jq -r '.name')
        if [ "$selected_item" = "$name" ]; then
            dep=$(echo "$item" | jq -r '.dep')
            if [ "$dep"!= "0" ]; then
                eval "$dep"
            fi
            if [ "$repo_type" = "gitee" ]; then
                eval "$(echo "$item" | jq -r '.command_gitee')"
            elif [ "$repo_type" = "github" ]; then
                eval "$(echo "$item" | jq -r '.command_github')"
            fi
        fi
    done
done